<?php

require_once ROOT_DIR.'models/User.php';

/**
 * Tries to delete the user with the given id and prints the view of the results.
 * @param int $id
 */
function deleteUser($id){
	//check user exists
	$user = User::getUser($id);
	if(!is_null($user)){
		if($_SESSION['USER_NAME'] == 'admin' && $user->getLoginName() != 'admin'){
			if($user->removeFromDB()){
				$res = new ResultJSON(false, session_id());
				$res->addItem('uID', $user->getID());
				echo $res->renderView();
			} else {
				$res = new ResultJSON(true, session_id(), ERROR_ACTION_FAILED);
				echo $res->renderView();
			}
		} else {
			$res = new ResultJSON(true, session_id(), ERROR_NOT_ALLOWED);
			echo $res->renderView();
		}
	} else {
		$res = new ResultJSON(true, session_id(), ERROR_USER_DOES_NOT_EXIST);
		echo $res->renderView();
	}
}

?>